/*
	Anonymous Image Board Image Downloader - program for downloading pictures from AIB
	Copyright (C) 2011-2012 Metallhead <Metallhead@megabox.ru>

	HomePage:    http://code.google.com/p/aibid/

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "feedback.h"
#include "ui_feedback.h"
#include <QDebug>

FeedBack::FeedBack(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::FeedBack)
{
    ui->setupUi(this);
	state = 0;
}

FeedBack::~FeedBack()
{
    delete ui;
}

void FeedBack::on_buttonBox_accepted()
{
	QUrl url("ftp://aibid.ru");
	if(url.isValid())
	{
		connect(&ftp, SIGNAL(commandFinished(int,bool)), this, SLOT(done(int, bool)));
		ftp.connectToHost(url.host(), url.port(21));
	}
}

void FeedBack::done(int, bool error)
{
	if(error)
	{
		showResult(error);
		return;
	}
	switch(state)
	{
	case 0:
		state++;
		ftp.login();
		break;
	case 1:
		state++;
		ftp.cd("/secret/aibid");
		break;
	case 2:
		state++;
		putFile(QString::number(QDate::currentDate().day()) + "." + QString::number(QDate::currentDate().month()) + "." + QString::number(QDate::currentDate().year()));
		break;
	case 3:
		state++;
		ftp.close();
		break;
	case 4:
		state = 0;
		showResult(error);
		break;
	}
}

void FeedBack::putFile(QString fileName)
{
	if(ui->sendLog->isChecked())
	{
		QFile file(Common::filePlace("logs/"+fileName));
		if(!file.exists())
		{
			file.open(QIODevice::WriteOnly);
			file.write("This log was created when someone was trying to send today's log");
			file.close();
		}

		file.open(QIODevice::ReadOnly);
		QByteArray array = file.readAll();
		file.close();
		array.append("\r\nMessage from user: ");
		array.append(ui->textEdit->toPlainText()+"\r\n");

		ftp.put(array, fileName + "("+QString::number(QTime::currentTime().hour()) + "." + QString::number(QTime::currentTime().minute()) + ")" + "_" + QString::number(qHash(array)) + ".log");
	}
	else
	{
		QByteArray array;
		array.append(ui->textEdit->toPlainText()+"\r\n");
		ftp.put(array, fileName + "("+QString::number(QTime::currentTime().hour()) + "." + QString::number(QTime::currentTime().minute()) + ")" + "_" + QString::number(qHash(array)) + ".message");
	}
}

void FeedBack::showResult(bool error)
{
	QMessageBox box;
	if(error)
	{
		box.setWindowTitle(tr("Error"));
		box.setText(tr("Error when trying to send message."));
	}
	else
	{
		box.setWindowTitle(tr("Information"));
		box.setText(tr("Your message was sucessfully sent to the author"));
	}
	box.setDefaultButton(QMessageBox::Ok);
	box.exec();
	disconnect(&ftp, SIGNAL(commandFinished(int,bool)), 0, 0);
	ui->textEdit->clear();
	ui->sendLog->setChecked(false);
}

void FeedBack::on_buttonBox_clicked(QAbstractButton *button)
{

}
